package top.tagao.m03_beHavioralPatterns.b_12_Iterator.social_networks;

import top.tagao.m03_beHavioralPatterns.b_12_Iterator.iterators.FacebookIterator;
import top.tagao.m03_beHavioralPatterns.b_12_Iterator.iterators.LinkedInIterator;
import top.tagao.m03_beHavioralPatterns.b_12_Iterator.iterators.ProfileIterator;
import top.tagao.m03_beHavioralPatterns.b_12_Iterator.profile.Profile;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Viktor_Liu
 * @Date 2022/8/14 23:36
 */
public class LinkedIn implements SocialNetwork{

    private List<Profile> profiles;

    public LinkedIn(List<Profile> cache) {
        if (cache != null) {
            this.profiles = cache;
        } else {
            this.profiles = new ArrayList<>();
        }
    }

    public Profile requestProfileFromLinkedIn(String profileEmail) {

        simulateNetworkLatency();
        System.out.println("LinkedIn: Loading profile '" + profileEmail + "' over the network...");

        return   findProfile(profileEmail);
    }

    public List<String> requestProfileFriendsFromLinkedIn(String profileEmail, String contactType) {

        simulateNetworkLatency();
        System.out.println("LinkedIn: Loading '" + contactType + "' list of '" + profileEmail + "' over the network...");
        Profile profile = findProfile(profileEmail);
        if (profile != null) {
            return profile.getContacts(contactType);
        }

        return  null;
    }

    private Profile findProfile(String profileEmail) {
        for (Profile profile : profiles) {
            if (profile.getEmail().equals(profileEmail)) {
                return profile;
            }
        }
        return null;
    }

    private void simulateNetworkLatency() {
        try {
            Thread.sleep(2500);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }


    @Override
    public ProfileIterator createFriendsIterator(String profileEmail) {
        return new LinkedInIterator(this, "friends", profileEmail);
    }

    @Override
    public ProfileIterator createCoworkersIterator(String profileEmail) {
        return new LinkedInIterator (this, "coworkers", profileEmail);

    }
}
